package org.itstep.stepup.dal.repositories.impl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.itstep.stepup.dal.entities.Test;
import org.itstep.stepup.dal.repositories.TestRepository;


public class TestRepositoryImpl implements TestRepository {

	private final SessionFactory sessionFactory;
	
	public TestRepositoryImpl(SessionFactory sessionFactory) {		
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Test getById(int id) {
		Session session = sessionFactory.openSession();
		try {
			Transaction transaction = session.beginTransaction();
			try {
				Test test = (Test)session.get(Test.class, id);
				transaction.commit();
				return test;
			} catch (HibernateException e) {
				transaction.rollback();
				throw e;
			}
		} finally {
			session.close();
		}		
	}

}
